package team.nmsg.ge.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ExceptionController implements ErrorController{

	private static Logger log = LoggerFactory.getLogger(ExceptionController.class);
	
	private static final String ERROR_PATH = "/error";  
	
    @RequestMapping(value = "/404")
    public String page404() {
        return "error/404";
    }

    @RequestMapping(value = "/500")
    public String page500() {
        return "error/500";
    }

    @RequestMapping(value = "/401")
    public String page401() {
        return "error/401";
    }

    @RequestMapping(value = ERROR_PATH )
	public ModelAndView error(HttpServletRequest request) {

		int status = -1;
		try {
			Object code = request.getAttribute("javax.servlet.error.status_code");
			status = Integer.parseInt(code.toString() );
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		ModelAndView mav = null;
		if (status == 404) {
			mav = new ModelAndView("error/404", map);
		} else if (status == 403) {
			mav = new ModelAndView("error/403", map);
		} else if (status == 500) {
			mav = new ModelAndView("error/500", map);
		} else {
			mav = new ModelAndView("error/error", map);
		}

		return mav;
	}

	@Override
	public String getErrorPath() {
		return ERROR_PATH;
	}

	
}
